﻿using System;
using UnityEngine;
using Utils;

namespace Combat
{
    public class Ring : MonoBehaviour
    {
        public Timer RecoverTimer { get; private set; }
        
        [SerializeField] private float recoverTime = 5f; 
        [SerializeField] private int heal = 1; 
        
        private Health _health;


        private void Awake()
        {
            _health = GetComponent<Health>();
            _health.HealthChanged += Health_HealthChanged;
            
            RecoverTimer = new Timer(recoverTime, Repair);
        }

        private void Update()
        {
            RecoverTimer.Tick();
        }

        private void Health_HealthChanged()
        {
            if (_health.HasFullHealth)
            {
                return;
            }
            
            RecoverTimer.Reset();
        }

        private void Repair()
        {
            _health.Heal(heal);

            if (!_health.HasFullHealth)
            {
                RecoverTimer.Reset();
            }
        }
    }
}